{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "\n",
    "x = torch.arange(4)\n",
    "torch.save(x, 'x-file')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:13:51.311952Z",
     "start_time": "2025-02-26T08:13:49.603062Z"
    }
   },
   "id": "7d46a02d55c79f91",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([0, 1, 2, 3])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = torch.load('x-file')\n",
    "x2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:14:33.273376Z",
     "start_time": "2025-02-26T08:14:33.258819Z"
    }
   },
   "id": "1bd8b21a46043a60",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([0, 1, 2, 3]), tensor([0., 0., 0., 0.]))"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = torch.zeros(4)\n",
    "torch.save([x, y],'x-files')\n",
    "x2, y2 = torch.load('x-files')\n",
    "(x2, y2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:15:00.929219Z",
     "start_time": "2025-02-26T08:15:00.920044Z"
    }
   },
   "id": "b048e03c4f6fe489",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'x': tensor([0, 1, 2, 3]), 'y': tensor([0., 0., 0., 0.])}"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydict = {'x': x, 'y': y}\n",
    "torch.save(mydict, 'mydict')\n",
    "mydict2 = torch.load('mydict')\n",
    "mydict2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:15:44.205903Z",
     "start_time": "2025-02-26T08:15:44.199081Z"
    }
   },
   "id": "cb5ea08925d39637",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Linear(20, 256)\n",
    "        self.output = nn.Linear(256, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.output(F.relu(self.hidden(x)))\n",
    "\n",
    "net = MLP()\n",
    "X = torch.randn(size=(2, 20))\n",
    "Y = net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:16:24.069278Z",
     "start_time": "2025-02-26T08:16:24.062349Z"
    }
   },
   "id": "26e8f3bd2a994b2f",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "OrderedDict([('hidden.weight',\n              tensor([[-0.2023, -0.2218, -0.0918,  ..., -0.1244, -0.0003, -0.1895],\n                      [ 0.0452, -0.1437,  0.0037,  ...,  0.1248, -0.1955,  0.1835],\n                      [ 0.0749, -0.1302,  0.1928,  ...,  0.0366,  0.1434,  0.0874],\n                      ...,\n                      [-0.0401, -0.1289,  0.0585,  ..., -0.0973, -0.1696,  0.0370],\n                      [-0.1469, -0.0641,  0.1658,  ..., -0.0556,  0.0625, -0.1887],\n                      [ 0.0789,  0.2212,  0.0334,  ...,  0.1550, -0.0448, -0.0311]])),\n             ('hidden.bias',\n              tensor([ 0.0427,  0.1433, -0.1630,  0.0400, -0.0971,  0.1007, -0.1017,  0.0201,\n                       0.0438,  0.1694,  0.2104,  0.0557, -0.0685,  0.1511, -0.1313,  0.2037,\n                       0.1467,  0.1044,  0.1629, -0.0746, -0.2154, -0.1531, -0.0459, -0.1133,\n                       0.0556, -0.1077,  0.2107, -0.1646, -0.2059,  0.0902, -0.0870, -0.1198,\n                       0.2209,  0.0119,  0.0531, -0.0657,  0.0057,  0.0805,  0.2222,  0.1791,\n                      -0.0015,  0.2009,  0.1471,  0.1328, -0.0614,  0.1071, -0.0740, -0.0361,\n                       0.1545,  0.1642, -0.2111,  0.2096, -0.2015, -0.1540, -0.0820,  0.0064,\n                       0.0328, -0.0710,  0.1500,  0.0140, -0.2177, -0.1276,  0.1635,  0.0056,\n                      -0.2080,  0.1049,  0.1962,  0.2051,  0.1483,  0.0149,  0.0264, -0.2037,\n                      -0.1407,  0.2143, -0.0457,  0.1605, -0.0945, -0.2099,  0.1058, -0.1029,\n                      -0.0225, -0.1356,  0.0252,  0.1436, -0.1427, -0.1346, -0.0560, -0.1945,\n                       0.1456,  0.1312, -0.0074,  0.0501, -0.0508,  0.0376, -0.0814, -0.2149,\n                       0.0207, -0.2178,  0.0336, -0.1436,  0.0583,  0.0901,  0.1131,  0.0281,\n                      -0.0434, -0.0152,  0.0704,  0.1380,  0.0637, -0.1629,  0.0678,  0.1809,\n                       0.0644,  0.0945,  0.0537, -0.2116,  0.0139, -0.1081,  0.1237,  0.0930,\n                       0.2108, -0.2132, -0.0061, -0.0010, -0.1156, -0.0895, -0.1314,  0.0831,\n                       0.0154,  0.1798,  0.1184,  0.1155,  0.0845, -0.2191,  0.0340,  0.1465,\n                      -0.0989, -0.0255, -0.0738,  0.1097, -0.1983,  0.0994, -0.1249, -0.2201,\n                      -0.0923,  0.1070,  0.0965,  0.1840,  0.1024, -0.0949,  0.1234, -0.1067,\n                       0.1661, -0.0964, -0.0121, -0.1870,  0.1567,  0.1696, -0.0982,  0.0667,\n                       0.0424, -0.1076, -0.1805,  0.1414,  0.1986,  0.0929, -0.1140, -0.1975,\n                       0.1640, -0.0556, -0.1512,  0.0122, -0.0596,  0.0987, -0.1467,  0.2224,\n                       0.1313, -0.1577,  0.0184,  0.0209,  0.1434,  0.1105,  0.0922,  0.2124,\n                      -0.0123, -0.0899,  0.0956, -0.0788,  0.0601,  0.0791,  0.1408,  0.1241,\n                       0.0522, -0.1455, -0.0788, -0.2202, -0.1224, -0.1547,  0.1894,  0.0067,\n                       0.0797,  0.0911, -0.0241,  0.1783,  0.1975, -0.1586,  0.0562,  0.1225,\n                      -0.1629,  0.0909, -0.0374, -0.0562, -0.1912,  0.0319,  0.0476,  0.1839,\n                       0.1603, -0.1557,  0.0135,  0.0209, -0.1131, -0.0999,  0.1305, -0.0609,\n                      -0.1721, -0.1605, -0.0548,  0.0986,  0.1930, -0.2129,  0.2232, -0.0268,\n                      -0.0573,  0.1208, -0.0762,  0.1849, -0.1643, -0.1901,  0.0669,  0.1146,\n                      -0.0599,  0.2095, -0.1515,  0.1066, -0.1784,  0.0335,  0.1232, -0.2188,\n                      -0.1015, -0.1722,  0.0042,  0.1733,  0.1831, -0.1023,  0.1081,  0.2173])),\n             ('output.weight',\n              tensor([[-0.0234,  0.0031,  0.0212,  ..., -0.0121,  0.0495,  0.0421],\n                      [ 0.0047,  0.0243, -0.0186,  ..., -0.0304,  0.0405, -0.0095],\n                      [ 0.0306, -0.0539,  0.0485,  ...,  0.0474,  0.0291,  0.0309],\n                      ...,\n                      [-0.0375,  0.0479, -0.0450,  ...,  0.0283,  0.0108, -0.0445],\n                      [ 0.0396,  0.0329,  0.0065,  ..., -0.0440,  0.0277, -0.0238],\n                      [ 0.0344, -0.0518,  0.0558,  ..., -0.0349, -0.0373,  0.0274]])),\n             ('output.bias',\n              tensor([ 0.0601,  0.0472, -0.0224,  0.0113, -0.0006,  0.0530, -0.0256,  0.0333,\n                       0.0139,  0.0055]))])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.state_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:16:46.273717Z",
     "start_time": "2025-02-26T08:16:46.261765Z"
    }
   },
   "id": "368d5c76f7596e24",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "torch.save(net.state_dict(), 'mlp.params')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:16:38.280713Z",
     "start_time": "2025-02-26T08:16:38.276329Z"
    }
   },
   "id": "add68dcabf5c46ea",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "MLP(\n  (hidden): Linear(in_features=20, out_features=256, bias=True)\n  (output): Linear(in_features=256, out_features=10, bias=True)\n)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clone = MLP()\n",
    "clone.load_state_dict(torch.load('mlp.params'))\n",
    "clone.eval()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:17:35.788771Z",
     "start_time": "2025-02-26T08:17:35.781719Z"
    }
   },
   "id": "2984d5730d38f87b",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[True, True, True, True, True, True, True, True, True, True],\n        [True, True, True, True, True, True, True, True, True, True]])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_clone = clone(X)\n",
    "Y_clone == Y"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T08:18:01.026591Z",
     "start_time": "2025-02-26T08:18:01.020930Z"
    }
   },
   "id": "6690a726730e6118",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "b76ff970480f693a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
